package operate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import model.ShareFileModel;
import model.client.LocalClientModel;
import model.command.CommandLogout;
import model.command.ConnectComand;
import model.command.LimitIpCommand;
import model.command.SendFileCommand;
import model.command.SendFileListCommand;

import common.Command;
import common.Server;
import common.ServerOperate;

/**
 * client 请求处理的实现.
 *
 * @author guanweichen
 * @version Jan 18, 2011
 */
public class LocalServerOperate extends ServerOperate {
	
	public LocalServerOperate(Server server,Socket client) {
		this.server = server;
		this.client = client;
	}
	
	/**
	 * 发送文件内容.
	 * 
	 * @param client
	 * @throws IOException 
	 */
	@Override
	public void sendLogFile(Socket client, String fileName, SendFileCommand sendFileCommand) {
		
		OutputStream outStream;
		try {
			outStream = client.getOutputStream();
			for (ShareFileModel shareFile : server.getListShareFile()) {
				if (fileName.equals(shareFile.getFileName())) {
					File file = new File(shareFile.getServerFilePath());
					FileInputStream inputStream = new FileInputStream(file);
					shareFile.setCommand(Command.GET_FILE);
					inputStream.skip(sendFileCommand.getFileSeek() > 0?sendFileCommand.getFileSeek() - 1:0 );// 跳过多少长度
					int i = inputStream.available();
					if (i > 0) {
						System.out.println("new array byte : "+i+1);
						byte []bs = new byte[i+1];
						while (inputStream.read(bs) > 0) {
							System.out.println("write length : " + i);
							if (bs.length != 0) {
								outStream.write(bs);
								outStream.flush();
							}
							i = inputStream.available();
							bs = new byte[i+1];
						}
					}
					outStream.close();
					break;
				}
			}
		} catch (IOException e) {
			System.out.println("ERROR: send message to Client error..........................");
			e.printStackTrace();
		}
	}
	
	/**
	 * 第一次连接服务器.
	 * 
	 * @param client
	 * @throws IOException
	 */
	@Override
	public void connect(Socket client, ConnectComand comand) {
		
		ObjectOutputStream objectOut;
		try {
			objectOut = new ObjectOutputStream(client.getOutputStream());
			// 增加连接人
			LocalClientModel localClient = new LocalClientModel(client.getInetAddress().toString(), comand.getPort());
			server.addClient(localClient);
			// 发送共享文件
			SendFileListCommand command = new SendFileListCommand();
			command.setListShareFile(server.getListShareFile());
			objectOut.writeObject(command);
			System.out.println("IP :" + localClient.getAddress() + " is Connected");
		} catch (IOException e) {
			System.out.println("ERROR: send message to Client error..........................");
			e.printStackTrace();
		}
	}

	@Override
	public void sendListShareFile(Socket client) {
		
		ObjectOutputStream objectOut;
		try {
			objectOut = new ObjectOutputStream(client.getOutputStream());
			// 发送共享文件
			SendFileListCommand command = new SendFileListCommand();
			command.setListShareFile(server.getListShareFile());
			objectOut.writeObject(command);
		} catch (IOException e) {
			System.out.println("ERROR: send message to Client error..........................");
			e.printStackTrace();
		}
	}

	@Override
	public void clientLogout(Socket client, CommandLogout command) {
		
		LocalClientModel localClient = new LocalClientModel(client.getLocalAddress().toString(), command.getPort());
		server.removeClient(localClient);
		server.removeFileUpNotifyByAddressAndPort(client.getLocalAddress().toString(), command.getPort());
	}

	@Override
	public void limitIpMessage(Socket client) {
		
		ObjectOutputStream objectOut;
		try {
			objectOut = new ObjectOutputStream(client.getOutputStream());
			// 通知client ip limit
			LimitIpCommand command = new LimitIpCommand();
			objectOut.writeObject(command);
		} catch (IOException e) {
			System.out.println("ERROR: send message to Client error..........................");
			e.printStackTrace();
		}
	}

}
